//
//  HSMainViewController.swift
//  HSMainViewControllerHandler
//
//  Created by haisheng huang on 15/12/10.
//  Copyright © 2015年 haisheng huang. All rights reserved.
//

// iOS > 7.0

import UIKit



class HSMainViewController: UIViewController, UIScrollViewDelegate, HSTabBarViewDelegate, HSSegmentViewDelegate {
    
    var contentView: UIScrollView? = nil
    var pageControl: HSPageControl? = nil

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        self.title = "HSMainViewController"
        
        //self.navigationController?.navigationBar.hidden = true
        
        
        HSMainViewHelper.sharedInstance.createTopSegmentView(self, titles: ["First", "Second", "Third"])
     
        let highlightColor:UIColor = UIColor(red: 1.0, green: 91.0/255.0, blue: 126.0/255.0, alpha: 1.0)
        let normalColor:UIColor = UIColor(white: 136.0/255.0, alpha: 1.0)
        let BarItems = [[TabItemInfoKey.title:"瘦身",
            TabItemInfoKey.normalImage:"tab_icon_slim",
            TabItemInfoKey.highlightedImage:"tab_icon_slim_h",
            TabItemInfoKey.normalColor:normalColor,
            TabItemInfoKey.highlightedColor:highlightColor],
            [TabItemInfoKey.title:"社区",
                TabItemInfoKey.normalImage:"tab_icon_community",
                TabItemInfoKey.highlightedImage:"tab_icon_community_h",
                TabItemInfoKey.normalColor:normalColor,
                TabItemInfoKey.highlightedColor:highlightColor],
            [TabItemInfoKey.title:"发现",
                TabItemInfoKey.normalImage:"tab_icon_discover",
                TabItemInfoKey.highlightedImage:"tab_icon_discover_h",
                TabItemInfoKey.normalColor:normalColor,
                TabItemInfoKey.highlightedColor:highlightColor],
            [TabItemInfoKey.title:"我的",
                TabItemInfoKey.normalImage:"tab_icon_mine",
                TabItemInfoKey.highlightedImage:"tab_icon_mine_h",
                TabItemInfoKey.normalColor:normalColor,
                TabItemInfoKey.highlightedColor:highlightColor]]
        
        HSMainViewHelper.sharedInstance.createBottomTabBarView(self, items: BarItems as NSArray)
        
        let firstViewController: FirstViewController = FirstViewController.initStoryboardViewController()
        let secondViewController: SecondViewController = SecondViewController.initStoryboardViewController()
        let thirdViewController: ThirdViewController = ThirdViewController.initStoryboardViewController()
        //let fourthViewController: FourthViewController = FourthViewController.initStoryboardViewController()
        
        
        HSMainViewHelper.sharedInstance.createSliderContentView(self, viewControllers: [firstViewController, secondViewController, thirdViewController])
        HSMainViewHelper.sharedInstance.currentChildViewController?.viewWillAppear(true)
        HSMainViewHelper.sharedInstance.currentChildViewController?.viewDidAppear(true)
        
        let pageControl: HSPageControl = HSPageControl(type: HSPageControlType.rightBottom, superView: self.view, numberOfPages: 3, currentPage: 0)
        pageControl.currentPageIndicatorTintColor = UIColor.blue
        pageControl.pageIndicatorTintColor = UIColor.gray
        
        self.pageControl = pageControl
        
        //print("main view controller: \(self.pageControl)")
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

    
    /**
     * TabBarViewDelegate
     **/
    func tabBarView(_ tabBarView: HSTabBarView, didSelectAtIndex index: Int) {
        
        print("tabBarView didSelect index \(index)")
        
        let fourthViewController: FourthViewController = FourthViewController.initStoryboardViewController()
        self.navigationController?.pushViewController(fourthViewController, animated: true)
        
    }
    
    func tabBarView(_ tabBarView: HSTabBarView, shouldSelectAtIndex index: Int) -> Bool {
        
        return true
    }
    
    /**
     * SegmentViewDelegate
     **/
    func segmentView(_ segmentView: HSSegmentView, didSelectedItemAtIndex index: Int) {
        print("segmentView didSelectedItemAtIndex \(index)")
        HSMainViewHelper.sharedInstance.moveToNextView(self.contentView!, index:index, completion: {finish in print("\(finish)")})
        self.pageControl?.currentPage = index
    }
    
    func segmentView(_ segmentView: HSSegmentView, shouldSelectedItemAtIndex index: Int) -> Bool {
        
        return true
    }
    
    /**
     * UIScrollViewDelegate
     **/
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        
        let index: Int = Int(scrollView.contentOffset.x/scrollView.frame.width)
        HSMainViewHelper.sharedInstance.setTopSegmentViewHighlightItemAtIndex(index)
        HSMainViewHelper.sharedInstance.viewControllerTransitionHandler(index)
        self.pageControl?.currentPage = index
    }
    
    
}
